package com.nmh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nmh.config.JWT;
import com.nmh.config.MD5;
import com.nmh.config.R;
import com.nmh.entity.User;
import com.nmh.service.userService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@Slf4j
@RequestMapping("/user")
@Api(tags="用户管理模块")
public class userController {
    @Autowired
    private userService userservice;
    @ApiOperation(value="用户登录接口")
    @PostMapping("/Login")
    public R<String> loginController(@Validated @RequestBody User users, HttpServletRequest request){
        String password= MD5.MD5s(users.getUserPassword());
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<User>()
                .eq(User::getUserAccount,users.getUserAccount());
        User u= userservice.getOne(wrapper);
        if(u!=null){
            if(u.getUserPassword().equals(password)){
                Map<String,Object> claims=new HashMap<>();
                claims.put("userName",u.getUserAccount());
                claims.put("userId",u.getUserId());
                String token= JWT.Tokenget(claims);
                request.getSession().setAttribute("token",token);
                return R.success(token);
            }else{
                return R.error("401","密码错误");
            }
        }
        return R.error("401","账号不存在");
    }
    /*
    用户注册接口
     */
    @ApiOperation(value="用户注册接口")
    @PostMapping("/Register")
    public R<User> registerController(@RequestBody User users){
        users.setUserPassword(MD5.MD5s(users.getUserPassword()));
            users.setUserStates(1);
            userservice.save(users);
            return R.success(users);
//        }
//        return R.error("账号已经存在");
    }
    /*
    分页查询用户接口
     */
    @ApiOperation(value="用户分页查询接口")
    @GetMapping("/Page")
    public R<Page<User>> page(int pageNumber, int pageSize,String userName,String userPhone,String userStates){
        Page<User> pageInfo=new Page<>(pageNumber,pageSize);
//        int userstatess=0;
        if(userStates!=null){
            if("\"\"".equals(userStates)) {
                LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                        .like(StringUtils.isNotEmpty(userName), User::getUserName, userName)
                        .like(StringUtils.isNotEmpty(userPhone), User::getUserPhone, userPhone);
                userservice.page(pageInfo, wrapper);
                return R.success(pageInfo);
            }
        }

        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<User>()
                .like(StringUtils.isNotEmpty(userName), User::getUserName,userName)
                .like(StringUtils.isNotEmpty(userPhone), User::getUserPhone,userPhone)
                .like(StringUtils.isNotEmpty(userStates), User::getUserStates,userStates);
            userservice.page(pageInfo,wrapper);
            return R.success(pageInfo);

    }
    /*
    修改人员信息接口
     */
    @ApiOperation(value="修改人员信息接口")
    @PutMapping("/update")
    public R<String> update(@RequestBody User users){
        if(users.getUserPhone()!=null){
            String sub=users.getUserPhone().substring(6);
            String password=MD5.MD5s(sub);
            users.setUserPassword(password);
        }
        userservice.updateById(users);
        return R.success("修改成功");
    }
    /*
    修改人员状态
     */
    @ApiOperation(value="修改人员状态接口")
    @PostMapping("/states")
    public R<String> states(Integer userId){
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<User>()
                .eq(User::getUserId,userId);
        User user=userservice.getOne(wrapper);
        if(user.getUserStates()==1){
            user.setUserStates(0);
        }else{
            user.setUserStates(1);
        }
        userservice.updateById(user);
        return R.success("修改成功");
    }
    /*
    新增人员接口
     */
    @ApiOperation(value="新增人员管理")
    @PostMapping("/save")
    public R<String> save(@RequestBody User users) {
        //判断手机号是否唯一
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<User>()
                .eq(User::getUserPhone,users.getUserPhone());
        List<User> list=userservice.list(wrapper);
        if(list.size()==0){
            int count=(int)userservice.count()+1;
            String Acoount="YZ"+"000"+count;
            users.setUserPassword(MD5.MD5s(users.getUserPhone().substring(6)));
            users.setUserStates(users.getUserStates());
            users.setUserAccount(Acoount);
            userservice.save(users);
            return R.success("新增成功");
        }
        return R.error("手机号已经存在","401");
    }
    /*
    退出登录接口
     */
    @ApiOperation(value="退出登录接口")
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清除用户ID
        request.removeAttribute("token");
        return R.success("退出成功");
    }
    /*
    获取全测试
     */
    @GetMapping("/getalll")
    public R<List<User>> getall(){
       List<User> list= userservice.list();
       return R.success(list);
    }
}
